﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;

using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    public class PhotoDal
    {
        public PhotoDal()
        {
        }


        #region 添加图片
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Photo model)
        {
            using (Entities db = new Entities())
            {
                var categoryIds = model.CategoryIds.ToArrInt();
                var categores = db.Categoryes.Where(x => categoryIds.Contains(x.Id));
                foreach (var v in categores)
                {
                    model.DeputyCategoryes.Add(v);
                }
                db.Photos.Add(model);

                db.SaveChangesAsync().Wait();
                return model.Id;
            }
        }
        #endregion

        #region 修改图片
        /// <summary>
        /// 修改图片
        /// </summary>
        /// <param name="model"></param>
        public void Edit(Photo model)
        {
            using (Entities db = new Entities())
            {
                var categoryIds = model.CategoryIds.ToArrInt();
                var categores = db.Categoryes.Where(x => categoryIds.Contains(x.Id));
                db.Database.ExecuteSqlCommand("DELETE FROM CMS_CoreCategories WHERE ContentId={0}", model.Id);
                foreach (var v in categores)
                {
                    model.DeputyCategoryes.Add(v);
                }

                db.Photos.Attach(model);
                db.Entry(model).State = EntityState.Modified;

                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 加载图片
        /// <summary>
        /// 加载图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Photo GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.Photos.Include(x => x.ContentPhotoLists).FirstOrDefault(x => x.Id == id);
            }
        }
        #endregion

        #region 绑定简单内容
        /// <summary>
        /// 绑定简单内容
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Photo> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = from x in db.Photos
                            where !x.IsDeleted && x.IsChecked
                            orderby x.Id descending
                            select x;
                return query.Skip(pp.Skip).Take(pp.PageSize).ToList();
            }
        }
        /// <summary>
        /// 统计内容数量，配合“绑定简单内容”
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            using (Entities db = new Entities())
            {
                return db.Photos.Count(x => !x.IsDeleted && x.IsChecked);
            }
        }
        #endregion
    }
}
